
#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
# @file   : pwd_strength.py
# @author : weichy
# @date   : 2019/01/03
# @version: 5.0
# @desc   :  测试密码的强度
            2.0循环的终止,限制密码的尝试次数
            3.0文件的操作
            4.0读取写入的密码文件
            5.0将相关的方法封装成整体 即类
'''

class PasswordTool:
    '''
    密码工具类
    '''
    def __init__(self,password):
        # 类的属性
        self.password = password
        self.strength_level = 0

    # 类的方法
    def check_number_exist(self):
        #判断字符串中是否含有数字
        has_number = False
        for c in self.password:
            if c.isnumeric():
                has_number = True
                break
        return has_number

    def check_letter_exist(self):
        #判断字符串中是否含有数字
        has_letter = False
        for c in self.password:
            if c.isalpha():
                has_letter = True
                break
        return has_letter

    def process_password(self):
        # 规则1： 密码长度大于8
        if len(self.password)>8:
            self.strength_level +=1
        else:
            print('密码长度要求至少8位。')
        # 规则2： 密码要包含数字
        if self.check_number_exist:
            self.strength_level +=1
        else: 
            print('密码中不包含数字。')
        # 规则3:密码要包含字母
        if self.check_letter_exist:
            self.strength_level +=1
        else: 
            print('密码中不包含字母。')
    






def main():

    try_times =5
    while try_times>0:
        password = input('请输入密码：')
        # 实例化密码工具对象
        password_tool = PasswordTool(password)
        password_tool.process_password()

        with open('password_3.0.txt','a') as f:
            f.write('密码：{}，强度{}。\n'.format(password,password_tool.strength_level))
            # f.readlines()
    #     # 密码强度
    #     strength_level = 0


    #     with open('password_3.0.txt', 'a') as f:   
    #         # print(f.read()) 
    #         f.write('密码：{}，强度：{}\n'.format(password,strength_level))
    #         f.close()           

        if password_tool.strength_level >=3:
            print('恭喜！密码强度合格。')
            break
        else:
            print('密码强度不合格。')
            try_times -=1
        print()

    if try_times <=0:
        print('尝试次数过多，密码设置失败。')
    # 读取文件
    # with  open('password_3.0.txt','r') as f:
    #     # content = f.read()
    #     # print(content)
    #     # 2.readline()
    #     # line=f.readline()
    #     # print(line)
    #     # line=f.readline()
    #     # print(line)
    #     # 3.readline()

    #     for line in f.readlines():
    #         print('read:{}'.format(line)



if __name__ == '__main__':
    main()